package com.zmn.plat.admin.controller.orderconfig.comment;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zmn.common.dto2.ResponseDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.utils.string.InitialUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.manager.dictionary.ProductDict;
import com.zmn.plat.model.entity.orderconfig.comment.OrderComment;
import com.zmn.plat.model.entity.orderconfig.comment.OrderCommentQuery;

/**
 * @author tanbiao
 * @description
 * @since 2019/10/25 9:12
 * @modifier
 **/
@RestController
@RequestMapping("/orderconfig/comment/")
@SuppressWarnings("Duplicates")
public class OrderCommentModifyController extends OrderCommentBaseController {
    private Logger logger = LoggerFactory.getLogger(OrderCommentModifyController.class);

    /**
     * region 新增/修改
     *
     * @param orderComment
     *            产品新增修改实体
     * @return ResultDTO 执行结果
     */
    @RequestMapping(value = "addEdit")
    @RequiresPermissions("/orderconfig/comment/detail.action")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody OrderComment orderComment) {
        String operator = getMcStaffRealName();
        String lable = "";
        try {
            OrderCommentQuery query = new OrderCommentQuery();
            query.setComment(orderComment.getComment());
            query.setId(orderComment.getId());
            OrderComment comment = orderCommentService.findByComment(query);
            if (null != comment) {
                return ResponseDTO.fail("评论内容不能重复！");
            }
            if (orderComment.getId() == null) {
                lable = "新增评论";
                orderComment.setCreater(operator);
                orderComment.setCreateTime(new Date());
                orderComment.setStatus(ProductDict.STATUS_ENABLE);
                orderComment.setFirstLetter(InitialUtil.getFristEnName(orderComment.getComment()));
                orderCommentService.insert(orderComment);
                return ResponseDTO.success(true,"新增成功！");
            } else {
                lable = "修改评论";
                orderComment.setUpdater(operator);
                orderComment.setUpdateTime(new Date());
                orderComment.setFirstLetter(InitialUtil.getFristEnName(orderComment.getComment()));
                orderCommentService.updateByKey(orderComment);
                return ResponseDTO.success(true,"修改成功！");
            }
        } catch (Exception ex) {
            logger.error(lable + "失败", ex);
            return ResponseDTO.fail(lable + "失败！");
        }
    }

    @RequestMapping(value = "updateStatus")
    @RequiresPermissions("/orderconfig/comment/list.action")
    public ResponseDTO updateStatus(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "ids", defaultValue = "") String ids, @RequestParam(value = "status", defaultValue = "0") int status) {
        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }

        setRequestAttributes(request, 121, ids, 0);

        List<Integer> reasonIds = StringUtil.convertStrsToList(ids);
        orderCommentService.updateStatusBatch(reasonIds, status, getMcStaffRealName());
        return ResponseDTO.success(true,"操作成功！");
    }

    /**
     * 描述:修改排序
     *
     * @param id
     *            分类id
     * @param sort
     *            修改的状态
     * @return: ResultDTO 执行结果
     * @auther: tanbiao
     * @since:20191022
     */
    @RequestMapping(value = "updateSort")
    @RequiresPermissions("/orderconfig/comment/list.action")
    public ResponseDTO updateSort(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "id", defaultValue = "0") Integer id, @RequestParam(value = "sort", defaultValue = "0") Integer sort) {
        if (id == 0) {
            return ResponseDTO.fail("请选择要更改排序的数据！");
        }
        if (sort == 0) {
            return ResponseDTO.fail("请设置排序值！");
        }
        orderCommentService.updateSortKey(id, sort, getMcStaffRealName());
        return ResponseDTO.success(true,"操作成功！");
    }

    /**
     * 描述:修改首字母
     *
     * @param id
     *            分类id
     * @param firstLetter
     *            首字母
     * @return: ResultDTO 执行结果
     * @auther: tanbiao
     * @since:20191022
     */
    @RequestMapping(value = "updateFirstLetter")
    @RequiresPermissions("/orderconfig/comment/list.action")
    public ResponseDTO updateFirstLetter(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "id", defaultValue = "0") Integer id,
        @RequestParam(value = "firstLetter", defaultValue = "") String firstLetter) {
        if (id == 0) {
            return ResponseDTO.fail("请选择要更改首字母的数据！");
        }
        if (StringUtil.isBlank(firstLetter)) {
            return ResponseDTO.fail("请设置分类首字母！");
        }
        orderCommentService.updateFirstLetterByKey(id, firstLetter, getMcStaffRealName());
        return ResponseDTO.success(true,"操作成功！");
    }

}